package com.zhm.demo.jdframe;

import io.github.burukeyou.dataframe.iframe.SDFrame;
import io.github.burukeyou.dataframe.iframe.item.FI2;
import io.github.burukeyou.dataframe.iframe.support.JoinOn;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

public class TestJdFrame {

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class UserVO {
        private Integer id;
        private String name;
        private Integer age;
        private Integer score;
    }
    // 添加订单数据模型用于join示例
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class OrderVO {
        private Integer id;
        private Integer userId;
        private BigDecimal amount;
    }

    private static final List<UserVO> userVOS = Arrays.asList(
            new UserVO(1, "张三", 20, 90),
            new UserVO(2, "李四", 21, 92),
            new UserVO(3, "王五", 30, 95),
            new UserVO(4, "赵六", 15, 85)
    );


    // 添加join示例 - 关联用户和订单数据
    public static final List<OrderVO> orderVOS = Arrays.asList(
            new OrderVO(1, 1, new BigDecimal(100)),
            new OrderVO(2, 1, new BigDecimal(200)),
            new OrderVO(3, 2, new BigDecimal(150)),
            new OrderVO(4, 3, new BigDecimal(300))
    );

    public static void main(String[] args) {

        // 创建用户和订单的SDFrame
        SDFrame<OrderVO> orderFrame = SDFrame.read(orderVOS);

// 查询分数在 60-90 之间的姓名和分数的 Top 5
        SDFrame<FI2<String, BigDecimal>> sdf1 = SDFrame.read(userVOS)
                .whereNotNull(UserVO::getScore)
                .whereBetween(UserVO::getScore, 60, 90)
                .groupBySum(UserVO::getName, UserVO::getScore)
                .sortDesc(FI2::getC2)
                .cutFirst(5);
        sdf1.show();

// 查询姓名以“张”开头的信息
        SDFrame<UserVO> sdf2 = SDFrame.read(userVOS)
                .whereNotNull(UserVO::getName)
                .whereLikeLeft(UserVO::getName, "张");
        sdf2.show();

// 计算分数大于 80 的平均年龄
        BigDecimal avgAge = SDFrame.read(userVOS)
                .whereNotNull(UserVO::getAge)
                .whereGt(UserVO::getScore, 80)
                .avg(UserVO::getAge);
        System.out.println(avgAge);

// 执行内连接操作，关联条件：user.id = order.userId
        SDFrame<FI2<UserVO, OrderVO>> joinedFrame = SDFrame.read(userVOS).join(orderFrame, JoinOn.on(UserVO::getId, OrderVO::getUserId));
        // 显示连接结果
        System.out.println("\n用户订单关联结果：");
        joinedFrame.forEachDo(row -> {
            UserVO user = row.getC1();
            OrderVO order = row.getC2();
            System.out.println(user + " 购买了 " + order);
        });

    }
}
